const util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {
    adressList: [],
    chooseYes: true, //从微信获取地址   锁
    imgUrl: getApp().globalData.imgUrl,
    adressList: [],
    page: 1,
    pageshow: 10,
    showAddreddwx:false
  },

  addAddrise() { //直接点击底部btn
    wx.navigateTo({
      url: '../ShouHuoDiZhiBianJi/ShouHuoDiZhiBianJi',
    })
  },
  jumpAdd(e) { //点击笔进入btn
    let url = `../ShouHuoDiZhiBianJi/ShouHuoDiZhiBianJi?adressInfo=${JSON.stringify(e.currentTarget.dataset.item)}`
    wx.navigateTo({
      url
    })
  },
  getAdress(e) { //调用系统权限获取地址
    let that = this
    wx.chooseAddress({
      success: (res) => {
        let { userName, telNumber, detailInfo, provinceName, cityName, countyName } = res;
        let data = {
          name: userName, //  用户名
          phone: telNumber, //  电话号
          address: detailInfo, //  详细地址
          position: `${provinceName}${cityName}${countyName}`, //  所在区域
          is_default: false + '', //  默认
          address_id: '', //  编辑或者新建地址
          tag: '', //  标签名
          flag: 'save',
          newtag: '' //  new标签名
        }
        util.API('spwx/v1.Address/setAddress', data).then(({
          code
        }) => {
          if (code == 200) {
            wx.showToast({
              title: '添加成功',
              duration: 3000,
            })
            that.getAdressList(false)
          } else {
            wx.showToast({
              title: '添加失败'
            })
          }
        })
        this.setData({
          add: false
        })
      },
      fail(res) {
        wx.getSetting({
          success (res) {
            if (!res.authSetting['scope.address']) {
              that.setData({
                chooseYes: false
              })
            }
          },
        })
      }
    })
  },

  openSetting() {
    var that = this
    wx.openSetting({
      success(settingData) {
        if (settingData.authSetting['scope.address']) {
          that.setData({
            chooseYes: true
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '获取权限失败,不能获取地址',
          })
        }
      }
    })
  },
  getAdressList(isBottom) { //请求地址列表
    let {page,pageshow} = this.data;
    util.API('spwx/v1.Address/addressList', {
      status: 'normal',
      page,
      pageshow
    }).then(({
      code,
      data,
      msg
    }) => {
      
      if (code == 200) {
        if (isBottom) {
          let newArr = this.data.adressList.concat(data.list)
          this.setData({
            adressList: newArr
          })
        } else {
          this.setData({
            adressList: data.list
          })
        }
      } else {
        wx.showToast({
          title: isBottom ? "没有更多数据了" : msg,
          icon: 'none'
        })
      }
      this.setData({
        showAddreddwx: true
      })
    })
  },

  chooseAdress(e) { //选择是否在配送范围内
    let pageinFo = getCurrentPages()[getCurrentPages().length - 2]
    // 如果 来自于订单页 就说明是选择配送范围
    if (pageinFo.route == 'pages/DingdanPay/DingdanPay') {
      util.API('pyapi/create_order', {
        type: this.data.type,
        goods: pageinFo.data.goods,
        address_id: e.currentTarget.dataset.id
      }, 'application/x-www-form-urlencoded').then(({
        code,
        totalFee,
        msg
      }) => {
        if (code == 200) {
          if (totalFee.code == 200) {
            // 如果请求回来运费 并且在配送范围内   -- >  操作上一页的数据  把地址带回去
            let pages = getCurrentPages(),
              currPage = pages[pages.length - 2];
            // currPage.data.Address.address_id = e.currentTarget.dataset.id
            currPage.setData({
              Address: totalFee.Address
            })
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showToast({
              title: totalFee.msg + '',
              icon: 'none',
              duration: 3000
            })
          }
        }else{
          wx.showToast({
            title:msg + '',
            icon:'none'
          })
        }
      })
    } else if (pageinFo.route='elsePage/pages/startBargain/startBargain'){
      console.log(this.data.adressList)
      console.log(e.currentTarget.dataset.idx)
      console.log(pageinFo)
      pageinFo.setData({
        Address: this.data.adressList[e.currentTarget.dataset.idx]
      })
      wx.navigateBack({
        delta:1
      })
    }
  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.getAdressList(true)
  },
  onLoad: function (options) {
    this.setData({
      type: options.type || ''
    })
  },
  onShow() {
    this.setData({
      page:1
    })
    this.getAdressList(false)
  }
}))